//Weapon Script Vers 0.2

//Global Vars
integer gWactive;		  		//Weapon Active
integer gUchannel = 66;			//User Listning Channel
integer gWchannel = 548934;		// weapons channel
string gWpdraw = "WpDraw";		// Draw animation
string gWpsheath = "WpSheath";	// Sheathe animation
string gWpidle = "WpIdle";		// Idle animation
string gWready = "WpReady";		// Ready animation
string gWpleft = "WpStkLeft";	// strike left animation
string gWpright = "WpStkRight";	// strike Right animation
string gWpfront = "WpStkFront";	// strike Front animation.
string gWpback = "WpStkback";	// strike Back animation
string gWpblock = "WpStkblock";	// Block animation
string wield = "sgl_";			// wielding set
integer cheat = FALSE;			// Cheat check
integer mainhand = TRUE;		// This weapon is a main hand weapon
//======================================================================================================================================
cheatcheck(string message)					// Cheat check Fuction;
{
	if(cheat == TRUE)								// Run Anti cheat check
	{
		// CCS KILL PERSON/ Zero hp,stam,racial and scream Cheater then detach
		string msg = "MY OWNER "+llKey2Name(llGetOwner()) +" AM A CCS WEAPONS CHEATER, AND DESERVE TO DIE";
		llRegionSay(0,msg);
		llDetachFromAvatar();
	}
	else if(llToLower(message) == "report5offhand") wield = "dual_";
	else if(llToLower(message) == "report6mainhand")
	{
		if(mainhand==TRUE)
		{
			cheat = TRUE;
			// CCS KILL PERSON/ Zero hp,stam,racial and scream Cheater then detach
			string msg = "MY OWNER "+llKey2Name(llGetOwner()) +" AM A CCS WEAPONS CHEATER, AND DESERVE TO DIE";
			llRegionSay(0,msg);
			llDetachFromAvatar();
		}
	}
	else cheat = FALSE;
}

default
{
	on_rez(integer params)
	{
		llStopSound(); 									// Initialize sound by stopping all of them.
		gWactive = FALSE;								// Initialize Weapons Active.
		cheatcheck("1");
	}
	state_entry()
	{
		llListen(gUchannel,"","","");		//Listen to User channel.
		llListen(gWchannel,"","","");		//Listen to Weapons channel.
	}

	attach(key attached)
	{
		string Agentname = llKey2Name(llGetOwner()); 	// get the agent name
		string Msg;										// local Message Variable
		if (llGetAttached()!= ATTACH_LHAND || ATTACH_RHAND)
		{
			Msg = ("I Have been rezzed on the wrong location by " + Agentname);
			llRegionSay(0, Msg );
			llDetachFromAvatar();
		}
		else if (llGetAttached()== ATTACH_LHAND)
		{
			if (mainhand==TRUE)
			{
				Msg = ("I Have been Attached to wrong hand by " + Agentname);
				llRegionSay(0, Msg );
				llDetachFromAvatar();
			}
		}
		else if (llGetAttached()== ATTACH_RHAND)
		{
			if (mainhand==FALSE)
			{
				Msg = ("I Have been Attached to wrong hand by " + Agentname);
				llRegionSay(0, Msg );
				llDetachFromAvatar();
			}
		}


		if(attached == llGetOwner())				// Check to see if the person attaching is the owner.
		{
			llListen(0,"",llGetOwner(),"");			// listen on channel zero for any chat spoken by the object owner.
			llSetStatus(STATUS_BLOCK_GRAB,TRUE);	// Block grabbing in this prim or the root if in a link_set
			llMessageLinked(LINK_SET,0,"OFF",NULL_KEY);		// Turn off whole linkset

		}
	}
	run_time_permissions(integer perms)
	{
		if(perms & PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION)		// If Runtime permissions equal PERMISSION_TAKE_CONTROLS then...
		{
			llTakeControls(
				CONTROL_LBUTTON |
				CONTROL_FWD |
				CONTROL_BACK |
				CONTROL_LEFT |
				CONTROL_RIGHT |
				CONTROL_DOWN |
				CONTROL_ROT_LEFT |
				CONTROL_ROT_RIGHT, TRUE, TRUE); 	// Intercept control for LeftMouse button, forward, back, left, right, down, rotate left, rotate right
		}
	}

	listen( integer channel, string name, key id, string msg )
	{
		if(llGetOwnerKey(id) == llGetOwner())		//if the message is from Owner...
		{
			if(channel==gWchannel) cheatcheck(msg);
			else if(llToLower(msg) == "draw")			//if owner says 'draw'
			{
				llMessageLinked(LINK_SET,0,"ON",NULL_KEY);		// turn on visible in weapon on
				gWactive = TRUE;									// Set Weapon active to true
				//llRequestPermissions(llGetOwner(),PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION); // ask for permissions to intercept Controls and Animations
				if (llGetPermissions()!=0)
				{ llTakeControls(
						CONTROL_LBUTTON |
							CONTROL_FWD |
							CONTROL_BACK |
							CONTROL_LEFT |
							CONTROL_RIGHT |
							CONTROL_DOWN |
							CONTROL_ROT_LEFT |
							CONTROL_ROT_RIGHT, TRUE, TRUE); 	// Intercept control for LeftMouse button, forward, back, left, right, down, rotate left, rotate right
						llStartAnimation(wield+gWpdraw);						//play idle weapon animation
						llStartAnimation(wield+gWpidle);						//play idle weapon animation
						llTriggerSound(wield+"snd_draw",1);					// Play draw sound
						//llLoopSound(wield+"chainsaw_idle",1);				// only used if the weapon have an idle sound, e.g. a chainsaw
					}
			}
			else if(llToLower(msg) == "sheath")
			{
				llMessageLinked(LINK_SET,0,"OFF",NULL_KEY); 						// Turn weapon visible off
				gWactive = FALSE;													// Set weapon Active to false
				//llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION); 	// ask for permissions to intercept Animations
				llStartAnimation(wield+gWpsheath);											// play sheath weapon animation
				llTriggerSound(wield+"snd_sheath",1);										// play sheath weapon sound
				llStopSound();														// Stop all sounds in object
				llReleaseControls();												// Release all controls requested
				llStopAnimation(wield+gWpsheath);											// Stop All animations in object
			}

		}
	}
	control(key id,integer level,integer edge)
	{
		if(level & CONTROL_LBUTTON)		// if left button is only pressed
		{
			llStopAnimation(wield+gWpidle);					// stop Idle animation
			llStartAnimation(wield+gWready);					// start ready animation
			if((edge & level & CONTROL_ROT_LEFT) | (edge & ~level & CONTROL_LEFT))		// if swing to left
			{
				llStopAnimation(wield+gWready);			// stop ready animation
				llTriggerSound(wield+"snd_wpstrike",1);		// trigger swing sound
				llStartAnimation(wield+gWpleft);			// start animation left
			}
			if((edge & level & CONTROL_ROT_RIGHT) | (edge & ~level & CONTROL_RIGHT))	// if swing to right
			{
				llStopAnimation(wield+gWready);
				llTriggerSound(wield+"snd_wpstrike",1);
				llStartAnimation(wield+gWpright);
			}
			if(edge & level & CONTROL_FWD)
			{
				llStopAnimation(wield+gWready);
				llTriggerSound(wield+"snd_wpstrike",1);
				llStartAnimation(wield+gWpfront);
			}
			if(edge & level & CONTROL_BACK)
			{
				llStopAnimation(wield+gWready);
				llTriggerSound(wield+"snd_wpstrike",1);
				llStartAnimation(wield+gWpback);
			}
			if(edge & level & CONTROL_DOWN)
			{
				llStopAnimation(wield+gWready);
				llTriggerSound(wield+"snd_block",1);
				llStartAnimation(wield+gWpblock);
			}
		}
		else if(~level & CONTROL_LBUTTON)
		{
			llStopAnimation(wield+gWready);
			llStartAnimation(wield+gWpidle);
		}

	}
}